package com.cy.pj.goods.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public class DefaultGoodsDao implements GoodsDao{

    @Autowired
    private DataSource dataSource;


    @Override
    public List<Map<String, Object>> findGoods() {

            Connection conn = null;
            Statement statement = null;
            ResultSet rs = null;
        try {
            //1.获取连接对象
            conn = dataSource.getConnection();
            //2.创建Statement对象
            statement = conn.createStatement();
            //3.发送sql
            String sql = "select * from tb_goods";
            rs = statement.executeQuery(sql);
//            String sql =
            //4.处理结果集
            List<Map<String,Object>> list = new ArrayList<>();
            ResultSetMetaData rsmd = rs.getMetaData();//获取结果集中的
            while (rs.next()) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", rs.getInt(1));
                map.put("name", rs.getString(2));
                map.put("remark",rs.getString(3));
                map.put("createdTime", rs.getString(4));
                list.add(map);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            //5.释放资源
            if(rs!=null)try{rs.close();}catch (SQLException e){}
            if(statement!=null)try{statement.close();}catch (SQLException e){}
            if(conn!=null)try{conn.close();}catch (SQLException e){}
        }
    }
}
